/* t_http.cpp : test dgnsc http
 * Copyright (C) 2011 drangon <drangon.zhou@gmail.com>
 * 2011-10
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <dgnsc/http.h>

#include "gtest.h"

#include <stdio.h>
// #include <assert.h>
#include <string.h>

static const char s_h1[] = "GET /index.html HTTP/1.1\r\n"
		"User-Agent: Firefoo/2.3  (hello world) \t \r\n"
		"long-HEAD:  \t This is long head \r\n"
		" with two\r\n"
		"\tline using LWS\r\n"
		"to-MOD: modify before\r\n"
		"to-MOD2: modify before\r\n"
		"to-Del: to be del\r\n"
		"to-Del2: to be del\r\n"
		"\r\nV";

TEST( http, http )
{
	int used_len = 0;
	dgn_http_t ht = dgn_http_init_parse( s_h1, &used_len );
	printf( "ht %p, used_len %d, remain [%s]\n", ht, used_len, s_h1 + used_len );
	EXPECT_TRUE( ht != NULL );

	printf( "method : [%s]\n", dgn_http_get_method( ht ) );
	printf( "uri    : [%s]\n", dgn_http_get_uri( ht ) );
	printf( "reqver : [%s]\n", dgn_http_get_request_ver( ht ) );

	dgn_http_iter_t iter = dgn_http_iter_next( ht, NULL );
	while( iter != NULL ) {
		const char * key = dgn_http_iter_get_key( ht, iter );
		const char * val = dgn_http_iter_get( ht, iter );
		printf( "header -- [%s] : [%s]\n", key, val );
		if( strcmp( key, "to-MOD" ) == 0 ) {
			dgn_http_iter_set( ht, iter, "new value of to-MOD" );
		}
		else if( strcmp( key, "to-Del" ) == 0 ) {
			iter = dgn_http_iter_del( ht, iter );
			continue;
		}

		iter = dgn_http_iter_next( ht, iter );
	}

	const char * p = NULL;
	p = dgn_http_get_header( ht, "long-header" );
	EXPECT_TRUE( p == NULL );
	p = dgn_http_get_header( ht, "long-head" );
	printf( "test get p [%s]\n", p );

	dgn_http_set_response( ht, "RTSP/2.0", 200, "OK" );

	dgn_http_set_header( ht, "to-mod2", "test mod2" );
	dgn_http_set_header( ht, "to-ADD", "test add" );
	dgn_http_del_header( ht, "to-DEL2" );

	dgn_str_t buf = dgn_str_init( 0 );
	dgn_http_output( ht, buf );
	printf( "output :[%s]\n", dgn_str_c( buf ) );
	dgn_str_fini( buf ), buf = NULL;
	dgn_http_fini( ht ), ht = NULL;
}

